package data.objects;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

import data.dynamodbfunctions.Persistence_Helper_PubEntry;
import data.exceptions.Dynamo_DB_Functions_Exception;
import data.meta.Metadata;
import data.meta.MetaAnnotations.MetaParam;
/**
 * Ein Thread_Entry Objekt
 * @author Alexander
 *
 */
@DynamoDBTable(tableName="CC_Thread_Entry")
public class PubEntry {
	private String Thread_ID;
	private String erstelldatum;
	private String TenantID;
	private String ersteller;
	private String admin;
	private String beschreibung;
	private String name;
	private String text;
	private Integer read_count;
			
	public PubEntry(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
		Date date = new Date();
		this.erstelldatum = ""+dateFormat.format(date);
		this.read_count = 0;
	}	
		
	public PubEntry(String thread_id,String tenant_id, String ersteller, String admin, String beschreibung,
			String name, String text) {
		super();
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
		Date date = new Date();
		this.Thread_ID = thread_id;
		this.erstelldatum = ""+dateFormat.format(date);
		this.TenantID=tenant_id;
		this.ersteller = ersteller;
		this.admin = admin;
		this.beschreibung = beschreibung;
		this.name = name;
		this.text = text;
		this.read_count = 0;
	}


	@DynamoDBHashKey(attributeName="ID")
	public String getThread_id() {return Thread_ID;}
	public void setThread_id(String thread_id) {this.Thread_ID = thread_id;}
	
	@DynamoDBRangeKey(attributeName="Range_ID")
	public String getErstelldatum() {return erstelldatum;}
	public void setErstelldatum(String erstelldatum) {this.erstelldatum = erstelldatum;}
	
	@DynamoDBAttribute(attributeName="TenantID")
	public String getTenant_id() {return TenantID;}
	public void setTenant_id(String thread_id) {this.TenantID = thread_id;}
		
	@DynamoDBAttribute(attributeName="Text")
	public String getText() {return text;}
	public void setText(String text) {this.text = text;}
	
	@DynamoDBAttribute(attributeName="Read_Count")
	public Integer getRead_count() {return read_count;}
	public void setRead_count(Integer read_count) {this.read_count = read_count;}
	
	@DynamoDBAttribute(attributeName="Ersteller")
	public String getErsteller() {return ersteller;}
	public void setErsteller(String ersteller) {this.ersteller = ersteller;}
	
	@DynamoDBAttribute(attributeName="Admin")
	public String getAdmin() {return admin;}
	public void setAdmin(String admin) {this.admin = admin;}
			
	@DynamoDBAttribute(attributeName="Beschreibung")
	public String getBeschreibung() {return beschreibung;}
	public void setBeschreibung(String beschreibung) {this.beschreibung = beschreibung;}
	
	@DynamoDBAttribute(attributeName="Name")
	public String getName() {return name;}
	public void setName(String forumname) {this.name = forumname;}
	
	@DynamoDBIgnore
	public static Metadata getMetaData(){
		Metadata meta= ClassSpy.getMetadata( "data.objects.PubEntry");
		return meta;
	}
//	@MetaMethod
	@DynamoDBIgnore
	public static PubEntry read(@MetaParam("Thread_ID") String thread, @MetaParam("ID") String id) throws Dynamo_DB_Functions_Exception {
		PubEntry readobject=Persistence_Helper_PubEntry.getInstance().read(thread,id);
		return readobject;
	}
//	@MetaMethod
	@DynamoDBIgnore
	public static List<PubEntry> read(@MetaParam("TenantID") String TenantID) throws Dynamo_DB_Functions_Exception {
		List<Select_Condition> conditionlist=new ArrayList<Select_Condition>();
		List<PubEntry> readobject=Persistence_Helper_PubEntry.getInstance().readwithAttributes(TenantID, conditionlist);
		return readobject;
	}
//	@MetaMethod
	@DynamoDBIgnore
	public static PubEntry delete(@MetaParam("Thread_ID") String thread, @MetaParam("ID") String id) throws Dynamo_DB_Functions_Exception {
		return Persistence_Helper_PubEntry.getInstance().delete(thread,id);
	}
//	@MetaMethod
	@DynamoDBIgnore
	public static PubEntry update(@MetaParam("Thread_ID") String thread, @MetaParam("ID") String id,
			@MetaParam("Tenant_ID") String Tenant_ID, @MetaParam("admin") String admin, 
			@MetaParam("name") String name, @MetaParam("beschreibung") String beschreibung, 
			@MetaParam("text") String text) throws Dynamo_DB_Functions_Exception {
		PubEntry updateEntry =Persistence_Helper_PubEntry.getInstance().read(thread,id);
		if(!"".equals(Tenant_ID)){
			updateEntry.setTenant_id(Tenant_ID);
		}
		if(!"".equals(admin)){
			updateEntry.setAdmin(admin) ;
		}
		if(!"".equals(beschreibung)){
			updateEntry.setBeschreibung(beschreibung);
		}
		if(!"".equals(name)){
			updateEntry.setName(name);
		}	
		if(!"".equals(text)){
			updateEntry.setText(text);
		}	
		return Persistence_Helper_PubEntry.getInstance().update(updateEntry);
	}
//	@MetaMethod
	@DynamoDBIgnore
	public static PubEntry create(@MetaParam("Thread_ID") String thread_id, 
			@MetaParam("Tenant_ID") String Tenant_ID, @MetaParam("ersteller") String ersteller, 
			@MetaParam("admin") String admin, @MetaParam("beschreibung") String beschreibung, 
			@MetaParam("name") String name, @MetaParam("text") String text)throws Dynamo_DB_Functions_Exception {
		PubEntry createEntry = new PubEntry(thread_id,Tenant_ID, ersteller, admin, beschreibung, name, text);
		return Persistence_Helper_PubEntry.getInstance().create(createEntry);	
	}
}
